package com.example.oasystem.controller.plan;

import com.example.oasystem.dao.login.AoaUserDao;
import com.example.oasystem.dao.plan.AoaPlanListDaoOfJian;
import com.example.oasystem.dao.status.AoaStatusListDao;
import com.example.oasystem.entity.page.Page;
import com.example.oasystem.entity.plan.AoaPlanListOfJian;
import com.example.oasystem.entity.status.AoaStatusList;
import com.example.oasystem.entity.user.AoaUser;
import com.example.oasystem.service.AoaPlanListServiceOfJian;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class PlanController {

    @Autowired
    private AoaUserDao aoaUserDao;
    @Autowired
    private AoaStatusListDao aoaStatusListDao;
    @Autowired
    private AoaPlanListServiceOfJian aoaPlanListServiceOfJian;
    @Autowired
    AoaPlanListDaoOfJian aoaPlanListDaoOfJian;
    @RequestMapping("/myplan")
    public String myplan(Model model){
        Page page =new Page(1,3,0,0,false,false);
        model.addAttribute("page",page);
        model.addAttribute("url","searchrealplantable");
        model.addAttribute("commentUrl","saveComment");
        return "plan/plantable";
    }

    @RequestMapping("/realplantable")
    public String realplantable(Model model, HttpSession session,
                                @RequestParam(value = "pageNum", required = false,defaultValue = "1") int pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "3") int pageSize,
                                @RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                                @RequestParam(value = "choose", defaultValue = "1")int choose,
                                @RequestParam (value = "starttime", defaultValue = "")String starttime,
                                @RequestParam (value = "endtime", defaultValue = "")String endtime){

        AoaUser user = (AoaUser)session.getAttribute("user");
        List<AoaUser> ulist = aoaUserDao.selectAllDept();
        List<AoaStatusList> status = aoaStatusListDao.selectStatusListByRealModel("aoa_plan_list");
        PageInfo<AoaPlanListOfJian> planList = null;
        if (choose == 1){
            planList = aoaPlanListServiceOfJian.selectUserNameAndPlanList(starttime, endtime, pageNum, pageSize, baseKey,user.getUserId(),13L);
        }else if (choose == 2){
            planList =aoaPlanListServiceOfJian.selectUserNameAndPlanList(starttime, endtime, pageNum, pageSize, baseKey,user.getUserId(),14L);
        }else if(choose == 3 ){
            planList =aoaPlanListServiceOfJian.selectUserNameAndPlanList(starttime, endtime, pageNum, pageSize, baseKey,user.getUserId(),15L);
        }


        List<AoaPlanListOfJian> uPlanList = planList.getList();
        List<AoaPlanListOfJian> realPlanList = new ArrayList<>();
        for (AoaPlanListOfJian each : uPlanList) {
            realPlanList.add(each);
        }

        Page page=new Page(planList.getSize(), Math.toIntExact(planList.getTotal()),planList.getPages(),planList.getPageNum(),planList.isIsFirstPage(),planList.isIsLastPage());
        model.addAttribute("ulist",ulist);
        model.addAttribute("status0",status);
        //System.out.println("realPlanList = " + realPlanList);
        model.addAttribute("uPlanList",realPlanList);
        model.addAttribute("commentUrl","saveComment");

        model.addAttribute("baseKey",baseKey);
        model.addAttribute("page",page);
        model.addAttribute("url","searchrealplantable");
        return "plan/realplantable";
    }
    @RequestMapping("/searchrealplantable")
    public String searchrealplantable(Model model, HttpSession session,
                                      @RequestParam(value = "pageNum", required = false,defaultValue = "1") int pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "3") int pageSize,
                                      @RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                                      @RequestParam(value = "choose", defaultValue = "1")int choose,
                                      @RequestParam (value = "starttime", defaultValue = "")String starttime,
                                      @RequestParam (value = "endtime", defaultValue = "")String endtime){
        AoaUser user = (AoaUser)session.getAttribute("user");
        List<AoaUser> ulist = aoaUserDao.selectAllDept();
        List<AoaStatusList> status = aoaStatusListDao.selectStatusListByRealModel("aoa_plan_list");
        PageInfo<AoaPlanListOfJian> planList = null;
        if (choose == 1){
            planList = aoaPlanListServiceOfJian.selectUserNameAndPlanList(starttime, endtime, pageNum, pageSize, baseKey,user.getUserId(),13L);
        }else if (choose == 2){
            planList =aoaPlanListServiceOfJian.selectUserNameAndPlanList(starttime, endtime, pageNum, pageSize, baseKey,user.getUserId(),14L);
        }else if(choose == 3 ){
            planList =aoaPlanListServiceOfJian.selectUserNameAndPlanList(starttime, endtime, pageNum, pageSize, baseKey,user.getUserId(),15L);
        }
        List<AoaPlanListOfJian> uPlanList = planList.getList();
        List<AoaPlanListOfJian> realPlanList = new ArrayList<>();
        for (AoaPlanListOfJian each : uPlanList) {
            realPlanList.add(each);
        }

        Page page=new Page(planList.getSize(), Math.toIntExact(planList.getTotal()),planList.getPages(),planList.getPageNum(),planList.isIsFirstPage(),planList.isIsLastPage());
        model.addAttribute("ulist",ulist);
        model.addAttribute("status0",status);
        //System.out.println("realPlanList = " + realPlanList);
        model.addAttribute("uPlanList",realPlanList);
        model.addAttribute("commentUrl","saveComment");

        model.addAttribute("baseKey",baseKey);
        model.addAttribute("page",page);
        model.addAttribute("url","searchrealplantable");
        return "plan/realplantable";
    }

    @RequestMapping("/down")
    public String down(String paid){
        return null;
    }

    @RequestMapping("/saveComment")
    public String saveComment(String pid,String comment){
        AoaPlanListOfJian updateObject = new AoaPlanListOfJian();
        updateObject.setPlanId(Long.parseLong(pid));
        updateObject.setPlanComment(comment);
        aoaPlanListDaoOfJian.updateById(updateObject);
        return "forward:/realplantable";
    }
}
